<?php
/**
 * 管理画面コントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Manage_Story extends  Controller {
	
        /*
	 * index
	 * @access  public
	 * @return  Response
	 */
	public function action_index() {
                
                $view = array();
                $query = DB::select('*');
                $query->from('bk_story');
                $result = $query->execute()->as_array();
                $view['col'] = $result; 
                return Response::forge(View::forge('manage/story/index',$view), D_HTTP_UPDATE);
	}

	/*
	 * left
         * @access  public
	 * @return  Response
	 */
	public function action_add() {
                
                $post = Input::post();
                $view = array();
                $view['message'] ='';
             
                $query = DB::select('*');
                $query->from('bk_catagories');
                $rs = $query->execute()->as_array();
                $view['category'] =$rs;
                if(isset($post['action']))
                {
                    $param = array();
                    $param['name'] = $post['name'];
                    $param['category_id'] = $post['catagory'];
                    $param['duration'] =isset($post['duration'])?$post['duration']:'';
                    $param['member_id'] ='admin';
                    $param['desc'] =isset($post['desc'])?$post['desc']:'';
                    
                    $query = DB::insert('bk_story');
                    
                    $query -> set($param);
                    $query -> execute();
                    
                   // var_dump($post);
                    $view['message']='successfully';
                 //   return;
                }
                
		return Response::forge(View::forge('manage/story/addnew',$view), D_HTTP_UPDATE);
	}
        public function action_submit()
        {
            
            return Response::redirect('/manage/admin/index');
            
        } 
        
        /*
         * 
         * search 
         */
        public function action_search()
        {
            $post=  Input::post();
            $view = array();
            
            if(isset($post['search']))
            {   
                $q = isset($post['q'])?$post['q']:'';
                
                $query = DB::select('*');
		$query -> from('bk_story');
		$query -> where(DB::expr('MATCH(name)'), '', DB::expr("AGAINST( " . DB::escape($q) . " IN BOOLEAN MODE)"));
		$result = $query -> execute() -> as_array();
                $view['data'] = $result;
                return Response::forge(View::forge('manage/story/search', $view), D_HTTP_UPDATE);
                
            }  else {
                return Response::forge(View::forge('manage/story/search'), D_HTTP_UPDATE);
            }
            
        }
        public function action_todo()
        {
           echo 'Đang phát triển , vui lòng chờ thêm thời gian!';
        }
        
	public function  action_del()
        {
               
                $get = Input::get();
                $id = $get['id']; 
                $query = DB::delete('bk_story');
                $query->where('id',$id);
                $query->execute();
                $view = array();
                $query = DB::select('*');
                $query->from('bk_story');
                $result = $query->execute()->as_array();
                $view['col'] = $result; 
                return Response::forge(View::forge('manage/story/index',$view), D_HTTP_UPDATE);
        }
        public function  action_modify()
        {
                $post = Input::post();
                $view = array();
                $view['message'] ='';
                if(isset($post['action']))
                {
                    $param = array();
                    $param['name'] = $post['name'];
                    $param['category_id'] = $post['catagory'];
                    $param['duration'] =isset($post['duration'])?$post['duration']:'';
                    $param['member_id'] ='admin';
                    $param['desc'] =isset($post['desc'])?$post['desc']:'';
                    
                    $query = DB::UPDATE('bk_story');
                    $query -> set($param);
                    $query->where('id',$post['id']);
                    $query -> execute();
                    $view['message']=$post['id'];
                   // return Response::forge(View::forge('manage/story/edit',$view), D_HTTP_UPDATE);
                }
                else
                {
                    $get = Input::get();
                    $id = $get['id'];
                    $query = DB::select('*');
                    $query->from('bk_story');
                    $query->where('id',$id);
                    $data=$query->execute()->as_array();

                    $view['data']=$data;
                    $query = DB::select('*');
                    $query->from('bk_catagories');
                    $rs = $query->execute()->as_array();
                    $view['category'] =$rs;
                    return Response::forge(View::forge('manage/story/edit',$view), D_HTTP_UPDATE);
                }
             return Response::redirect('manage/story');
        }

}
